﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS.Admin
{
    public partial class AlbumsManager : System.Web.UI.Page
    {
        public int PageNumber
        {
            set
            {
                ViewState["page"] = value;
            }
            get
            {
                if (ViewState["page"] == null)
                {
                    return 1;
                }
                else
                {
                    int page = Convert.ToInt32(ViewState["page"]);
                    return page;
                }
            }
        }
        public int PageSize
        {
            get
            {
                return 5;
            }
        }
        public int PageCount
        {
            set
            {
                ViewState["count"] = value;
            }
            get
            {
                if (ViewState["count"] == null)
                {
                    return 1;
                }
                else
                {
                    int page = Convert.ToInt32(ViewState["count"]);
                    return page;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageNumber = 1;
                AlbumsBinder();
            }
        }

        private void AlbumsBinder()
        {
            List<Albums> list = AlbumBLL.GetAlbumsList();
            var albumsList = list.Select(a => new
            {
                AlbumId = a.AlbumId,
                Title = a.Title,
                ArtistName = ArtistBLL.GetArtistsById(a.ArtistId).Name,
                GenreName = GenreBLL.GetGenresById(a.GenreId).Name,
                Price = a.Price
            }).ToList();
            repAlbumsList.DataSource = albumsList.Skip((PageNumber - 1) * PageSize).Take(PageSize).ToList();
            repAlbumsList.DataBind();
       
            PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(albumsList.Count) / 10));
            lblPager.Text = string.Format("每页{0}条/共{1}条 第{2}页/共{3}页", PageSize, albumsList.Count, PageNumber, PageCount);

            btnFirst.Enabled = !(PageNumber <= 1);
            btnPrev.Enabled = !(PageNumber <= 1);
            btnNext.Enabled = !(PageNumber >= PageCount);
            btnLast.Enabled = !(PageNumber >= PageCount);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AlbumsView.aspx");
        }

        protected void btnEdit_Command(object sender, CommandEventArgs e)
        {
            string url = string.Format("AlbumsView.aspx?aid={0}", e.CommandArgument);
            Response.Redirect(url);
        }

        protected void btnRemove_Command(object sender, CommandEventArgs e)
        {
            int albumId = Convert.ToInt32(e.CommandArgument);
            if (AlbumBLL.RemoveAlbum(albumId))
            {
                PageNumber = 1;
                AlbumsBinder();
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PageNumber = 1;
            AlbumsBinder();
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            PageNumber--;
            AlbumsBinder();
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PageNumber++;
            AlbumsBinder();
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PageNumber = PageCount;
            AlbumsBinder();
        }
    }
}